package GameMain.View.PTGame
{
	import GameMain.View.PTGame.GameManager;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class HurtValueNum extends Sprite
	{
		private static var addNumList:Array = new Array;  //血量加
		private static var subNumList:Array = new Array;  //血量减
		
		
		private var tickTime:int = 0;
		private var numList:Array = new Array();
		private var isAdd:Boolean = false;
		private var _timer:Timer = new Timer(30);
		
		
		public function HurtValueNum()
		{
			super();
			this._timer.addEventListener(TimerEvent.TIMER,_onEnterFrame);
		}
		
		public function setNum(num:int,bADD:Boolean):void
		{
			this.isAdd = bADD;
			var numAr:Array = this.splite(num);
			var numMC:MovieClip = this.getNum(-1);
			this.addChild(numMC);
			var posx:Number = 25;
			for(var i:int = numAr.length - 1;i>=0;i--)
			{
				numMC = this.getNum(numAr[i]);
				this.addChild(numMC);
				numMC.x = posx;
				posx += 25;
			}
		}
		
		public function addToParent(_parent:DisplayObjectContainer,_x:Number,_y:Number):void
		{
			this.x = _x;
			this.y = _y;
			_parent.addChild(this);
			this._timer.start();
		}
		
		
		private function _onEnterFrame(e:Event):void
		{
			this.tickTime++;
			this.y -=5;
			if(this.tickTime == 20)
			{
				this._timer.stop();
				this._timer.removeEventListener(TimerEvent.TIMER,_onEnterFrame);
				this.clear();
			}
		}
		
		
		private function splite(num:int):Array
		{
			var ar:Array = new Array();
			while(num >= 10)
			{
				ar.push(num % 10);
				num = int(num/10);
			}
			ar.push(num);
			return ar;
		}
		
		private function getNum(num:int):MovieClip
		{
			var numMC:MovieClip;
			if(this.isAdd)
			{
				if(addNumList.length == 0)
					this.createNewAddNum();
				numMC = addNumList.shift();
			}
			else
			{
				if(subNumList.length == 0)
					this.createNewSubNum();
				numMC = subNumList.shift();
			}
			if(num < 0)
				numMC.gotoAndStop(11);
			else if (num == 0)
				numMC.gotoAndStop(10);
			else
				numMC.gotoAndStop(num);
			this.numList.push(numMC);
			return numMC;
		}
		
		private function createNewAddNum():void
		{
			var addClass:Class = GameManager.AddNumClass;
			var numMC:MovieClip = new addClass as MovieClip;
			addNumList.push(numMC);
		}
		
		private function createNewSubNum():void
		{
			var subClass:Class = GameManager.SubNumClass;
			var numMC:MovieClip = new subClass as MovieClip;
			subNumList.push(numMC);
		}
		
		private function clear():void
		{
			while(this.numChildren)
			{
				this.removeChildAt(0);
			}
			while(this.numList.length)
			{
				if(this.isAdd)
					addNumList.push(this.numList.shift());
				else
					subNumList.push(this.numList.shift());
			}
			if(this.parent)
				this.parent.removeChild(this);
		}
		
		
	}
}